package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.education.convert.EduResourceFileConvert;
import com.engine4cloud.tdf.education.dto.EduResourceFileDTO;
import com.engine4cloud.tdf.education.dto.query.EduResourceFileQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduResourceFileResultDTO;
import com.engine4cloud.tdf.education.entity.EduResourceFileEntity;
import com.engine4cloud.tdf.education.mapper.EduResourceFileMapper;
import com.engine4cloud.tdf.education.service.EduResourceFileService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 学习资源-文件表
 *
 * @author wenjie
 * @date 2024-07-31 11:57:16
 */
@Service
public class EduResourceFileServiceImpl extends ServiceImpl<EduResourceFileMapper, EduResourceFileEntity> implements EduResourceFileService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(EduResourceFileQueryDTO dto, List<EduResourceFileDTO> fileList, TdfUser user) {
        this.saveBatch(EduResourceFileConvert.INSTANCE.createList(dto, fileList, user));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(EduResourceFileQueryDTO dto, List<EduResourceFileDTO> fileList, TdfUser user) {
        List<EduResourceFileResultDTO> existedList = this.findList(dto);
        List<EduResourceFileEntity> preSaveFileList = this.preSaveFileList(dto, fileList, user);
        if (CollectionUtils.isNotEmpty(preSaveFileList)) {
            this.saveBatch(preSaveFileList);
        }

        List<Long> preDeleteFileList = this.preDeleteFileList(existedList, fileList);
        if (CollectionUtils.isNotEmpty(preDeleteFileList)) {
            this.removeBatchByIds(preDeleteFileList);
        }
    }

    @Override
    public List<EduResourceFileResultDTO> findList(EduResourceFileQueryDTO dto) {
        List<EduResourceFileEntity> list = super.list(wrapper(dto));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(EduResourceFileConvert.INSTANCE::convert).toList();
    }

    private List<Long> preDeleteFileList(List<EduResourceFileResultDTO> existedList, List<EduResourceFileDTO> fileList) {
        if (CollectionUtils.isEmpty(existedList)) {
            return Collections.emptyList();
        }

        Map<Long, EduResourceFileDTO> modelFileMap = fileList.stream().filter(model -> Objects.nonNull(model.getId())).collect(Collectors.toMap(EduResourceFileDTO::getId, Function.identity(), (v1, v2) -> v1));
        return existedList.stream().map(EduResourceFileResultDTO::getId).filter(id -> !modelFileMap.containsKey(id)).toList();
    }

    private List<EduResourceFileEntity> preSaveFileList(EduResourceFileQueryDTO dto, List<EduResourceFileDTO> fileList, TdfUser user) {
        if (CollectionUtils.isEmpty(fileList)) {
            return Collections.emptyList();
        }

        return fileList.stream().filter(file -> Objects.isNull(file.getId())).map(file -> EduResourceFileConvert.INSTANCE.create(dto.getResourceCode(), file, user)).toList();

    }

    private LambdaQueryWrapper<EduResourceFileEntity> wrapper(EduResourceFileQueryDTO query) {
        LambdaQueryWrapper<EduResourceFileEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(EduResourceFileEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(EduResourceFileEntity::getCode, v));
        Queries.accept(query.getResourceCode(), v -> wrapper.eq(EduResourceFileEntity::getResourceCode, v));
        return wrapper;
    }
}